<?php
class CouponAction extends CommonAction
{
    function _initialize(){
        parent::_initialize();
        $this->dao = D('coupon');// 编辑、删除操作需要
    }

    // coupon表的增删改查
    public function index(){
        $this->dao = D('coupon');
        $this->listmap .= " and site_id=" . $this->siteid;

        parent::_list("","",$field="id,title,type,ctime,start_time,end_time,status");

        $this->display();
    }
    public function add()
    {
        // 设置当前时间为开始时间和结束时间
        $info['start_time'] = $info['end_time'] = time();
        $this->assign("info",$info);
        $this->assign("post_method","insert");
        $this->assign("ctime",time());
        $this->display();
    }
    public function insert(){
        $data = $_POST;
        $data['ctime'] = $data['ctime'];
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        parent::insert($data,U('index'));
    }

    public function edit(){
        $this->assign("post_method","update");

        parent::edit('add');
    }

    public function update(){
        $data = $_POST;
        // $data['ctime'] = strtotime($data['ctime']);
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        parent::update($data,U('index'));
    }
    public function adjust(){
        $map['id'] = $id = intval($_GET['id']);
        $coupon = $this->dao->where($map)->find();
        $result = $this->dao->adjustSn($coupon['num'],$id);
        if($result){
            $this->success("调整成功");
        }else{
            $this->error($this->dao->getError());
        }
    }
    public function load(){
        $id = intval($_GET['id']);
        import("@.ORG.Coupon.Coupon");
        $Coupon = new Coupon($id,"0");
        $Coupon->download();
    }
}